#include <stdio.h>
#include <stdlib.h>
void _merg(int* arr, int left, int right, int* tmp)
{
	if (left >= right)
		return;
	int mid = (left + right) >> 1;
	_merg(arr, left, mid, tmp);
	_merg(arr, mid + 1, right, tmp);
	int begin1 = left, end1 = mid;
	int begin2 = mid + 1, end2 = right;
	int index = left;
	while (begin1 <= end1 && begin2 <= end2)
	{
		if (arr[begin1] > arr[begin2])
		{
			tmp[index++] = arr[begin2];
			begin2++;
		}
		else
		{
			tmp[index++] = arr[begin1];
			begin1++;
		}
	}
	while (begin1 <= end1)
	{
		tmp[index++] = arr[begin1];
		begin1++;
	}while (begin2 <= end2)
	{
		tmp[index++] = arr[begin2];
		begin2++;
	}
	for (int i = left; i < right +1; i++)
	{
		arr[i] = tmp[i];
	}
}
void MergSort(int* arr, int n)
{
	int* tmp = (int*)malloc(sizeof(int) * n);
	_merg(arr, 0, n-1, tmp);
	free(tmp);
}
int main()
{
	int arr[] = { 5,6,8,1,3,9,7,6 };
	MergSort(arr, sizeof(arr) / sizeof(arr[0]));
	for (int i = 0; i < sizeof(arr) / sizeof(arr[0]); i++)
	{
		printf("%d ", arr[i]);
	}
	return 0;
}